﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace DataImporter
{
    class Program
    {
        const string _PROCESSEDDIR = "Processed";
        const string _FAILDIR = "Failure";
        static string _DataDirectory = null;
        static MySqlConnection conn = new MySqlConnection("Server=localhost;Database=rsp;Uid=rsp_user;Pwd=123456;charset=utf8;");

        static void Main(string[] args)
        {
            // Input the data directory
            Console.Write("Directory: ");
            _DataDirectory = Console.ReadLine();
            DirectoryInfo dir = new DirectoryInfo(_DataDirectory);

            // Check if folders exists
            if(!Directory.Exists(_DataDirectory + "\\" + _PROCESSEDDIR))
            {
                dir.CreateSubdirectory(_PROCESSEDDIR);
            }
            if (!Directory.Exists(_DataDirectory + "\\" + _FAILDIR))
            {
                dir.CreateSubdirectory(_FAILDIR);
            }
            
            // Acquire files from data directory
            FileInfo[] files = dir.GetFiles();

            // Open the connection
            conn.Open();
            Console.WriteLine("Running ..");

            // Traverse the files
            foreach (FileInfo file in files)
            {
                // Error mark
                bool failure = false;

                // Process the current file
                StreamReader reader = file.OpenText();
                try
                {
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        // Log: print the connection
                        Console.WriteLine(file.Name + " -> " + line);

                        // Configure command
                        MySqlCommand cmd = new MySqlCommand();
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.CommandText = "UpdateConnection";
                        cmd.Parameters.AddWithValue("?TitleEntryLabel", file.Name);
                        cmd.Parameters.AddWithValue("?ContentEntryLabel", line);
                        cmd.Connection = conn;

                        // Excute the update command
                        cmd.ExecuteNonQuery();

                        // Read the next line
                        line = reader.ReadLine();
                    }
                }
                catch (SystemException e)
                {
                    failure = true;

                    // Log: fail the file
                    Console.WriteLine("ERROR @ \"" + file.Name + "\": " + e.Message);
                }
                finally
                {
                    // Close the reader
                    reader.Close();
                }

                // Check failure
                if(failure)
                {
                    // Move the file to the failure folder
                    file.MoveTo(_DataDirectory + "\\" + _FAILDIR + "\\" + file.Name);
                }
                else
                {
                    // Move the file to the processed folder
                    file.MoveTo(_DataDirectory + "\\" + _PROCESSEDDIR + "\\" + file.Name);
                }
            }

            // Close the connection
            conn.Close();

            // Log: Finished
            Console.Write("Finished ..");
            Console.ReadKey();
        }
    }
}
